<?php
include('includes/config.php');
include("includes/functions.php");	
date_default_timezone_set('Europe/Bucharest');



	if(@$_GET['actiune']){
		switch($_GET['actiune']){
			case "add":
			/*
				$data = file_get_contents("php://input");
				$data = urldecode($data);
				$data = substr($data,stripos($data,"{"));

				$file = "data.txt";
				$fh = fopen($file, 'w') or die("can't open file");
				fwrite($fh, $_POST);
				fclose($fh);
			*/
				
				if(@$_POST){
				/*
				$data = json_decode($data);
				$lat = mysql_real_escape_string($data->{"lat"});
				$lon	= mysql_real_escape_string($data->{"lon"});
				$magnitude = mysql_real_escape_string($data->{"magnitude"});
				$time	= mysql_real_escape_string($data->{"time"});
				$region	= mysql_real_escape_string($data->{"region"});
				$review  =  mysql_real_escape_string($data->{"review"});
				$depth =  mysql_real_escape_string($data->{"depth"});
				*/
				$lat = mysql_real_escape_string($_POST['lat']);
				$lon	= mysql_real_escape_string($_POST['lon']);
				$magnitude = mysql_real_escape_string($_POST['magnitude']);
				$time	= mysql_real_escape_string($_POST['time']);
				$region	= mysql_real_escape_string($_POST['region']);
				$review  =  mysql_real_escape_string($_POST['review'] );
				$depth =  mysql_real_escape_string($_POST['depth']);
				
				if(@$_POST['software']){
					$tipSoftware = mysql_real_escape_string($_POST['software']);
					if($tipSoftware == "ews"){
						$ordineSoftware = "3";
					}else{
						$ordineSoftware = "1";
					}
				}else{
					$tipSoftware = "antelope";
					$ordineSoftware = "2";
				}
				$radius = "111";  //Kilometri
				$dataArr = array("lat"=>$lat,"lon"=>$lon,"magnitude"=>$magnitude,"time"=>$time,"region"=>$region,"review"=>$review,"depth"=>$depth);
				switch($review){
					case "y":
						$tip = "M";
					break;
					case "Y":
						$tip = "M";
					break;
					case "1":
						$tip = "M";
					break;
					case "m":
						$tip = "M";
					break;
					case "M":
						$tip = "M";
					break;
					default:
						$tip = "A";
					break;
				}
				$data_ora =  date("Y-m-d H:i:s",$time); 

				$res = mysql_query("SELECT *, ((ACOS(SIN('$lat' * PI() / 180) * SIN(lat * PI() / 180) + COS('$lat' * PI() / 180) * COS(lat * PI() / 180) * COS(('$lon' - lon) * PI() / 180)) * 180 / PI()) * 60 * 1.1515) AS `distance` FROM `evenimente` HAVING distance <= '$radius' and data_eveniment BETWEEN DATE_ADD(now(), INTERVAL -30 SECOND) AND DATE_ADD(now(), INTERVAL 30 SECOND) ORDER BY distance ASC") or die(mysql_error());
					$arrID = array();
					$mysqlErrArr = array();
				if(mysql_num_rows($res)==0){
					mysql_query("insert into evenimente (id_cutremur,data_eveniment,mag,adancime,lat,lon,regiune,tip,tipSoftware,ordineSoftware) VALUES (null,'$data_ora','$magnitude','$depth','$lat','$lon','$region','$tip','$tipSoftware','$ordineSoftware')");
					$arrID[] = mysql_insert_id();
					$mysqlErrArr[] = mysql_error();
					$region = strtoupper($region);

						$url = "http://www.lucianblog.ro/alerta.php";
						$name = "eveniment";
						gtJson($url,$name,$dataArr);
						

						$url = "http://eida.infp.ro/db_evenimente.php";
						$data = array(
										"addEvents"=>"da",
										"lat"=>$lat,
										"lon"=>$lon,
										"magnitude"=>$magnitude,
										"time"=>$data_ora,
										"region"=>$region,
										"review"=>$review,
										"depth"=>$depth,
										"tip"=>$tip,
										"tipSoftware" => $tipSoftware
										);
						
						$data_string = http_build_query($data);
						$ch=curl_init($url);
						curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
						curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
						curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
						$json_response = curl_exec($ch);
						#$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
						curl_close($ch);

					
				}else{
					while($row = mysql_fetch_array($res)){
						$id_eveniment = $row['id_cutremur'];
						mysql_query("UPDATE evenimente set
							data_eveniment = '$data_ora',
							mag = '$magnitude',
							adancime = '$depth',
							lat = '$lat',
							lon ='$lon',
							regiune = '$region',
							tip = '$tip',
							tipSoftware = '$tipSoftware',
							ordineSoftware = '$ordineSoftware',
							where id_cutremur = '$id_eveniment'
						");
						$arrID[] = $id_eveniment;
						$mysqlErrArr[] = mysql_error();
					
					}
				}

	

				if(empty($mysqlErrArr)){
					header('HTTP/1.1 200 OK', true, 200);
					$arr['succes'] = '0';
					$arr['error'] = $mysqlErrArr;
					echo json_encode($arr);
				}else{
					header('HTTP/1.1 201 Created', true, 201);
					$arr = array();
					$arr['succes'] = '1';
					$arr['id'] = $arrID;
					echo json_encode($arr);
				}
			}else{
					$mysqlErrArr = "Nu s au primit date!";
					header('HTTP/1.1 200 OK', true, 200);
					$arr['succes'] = '0';
					$arr['error'] = $mysqlErrArr;
					echo json_encode($arr);
			}
			break;
			
			case "getData":
			
			/*
			$de_la_data = $("#de_la_data").val();
			$pana_la_data = $("#pana_la_data").val();
			$magnitudine = $("#magnitudine").val();
			$adancimeDeLa = $("#adancimeDeLa").val();
			$adancimePanaLa = $("#adancimePanaLa").val();
			$nord = $("#nord").val();
			$vest = $("#vest").val();
			$est = $("#est").val();
			$sud = $("#sud").val();
			*/
			
			if(@$_GET['de_la_data']){
				$de_la_data = mysql_real_escape_string($_GET['de_la_data']);
			}else{
				$de_la_data = date("Y-m-d");
			}
			
			if(@$_GET['pana_la_data']){
				$pana_la_data = mysql_real_escape_string($_GET['pana_la_data']);
			}else{
				$pana_la_data = date("Y-m-d",strtotime(date("Y-m-d")) + 86400);
			}
			
			if(@$_GET['magnitudine']){
				$magnitudine = mysql_real_escape_string($_GET['magnitudine']);
			}else{
				$magnitudine = 3;
			}
			
			if(@$_GET['adancimeDeLa']){
				$adancimeDeLa = mysql_real_escape_string($_GET['adancimeDeLa']);
			}else{
				$adancimeDeLa = 45;
			}
			
			if(@$_GET['adancimePanaLa']){
				$adancimePanaLa = mysql_real_escape_string($_GET['adancimePanaLa']);
			}else{
				$adancimePanaLa = 150;
			}
			
			if(@$_GET['nord']){
				$nord = mysql_real_escape_string($_GET['nord']);
			}else{
				$nord = 90;
			}
			
			if(@$_GET['vest']){
				$vest = mysql_real_escape_string($_GET['vest']);
			}else{
				$vest = -180;
			}
			
			if(@$_GET['est']){
				$est = mysql_real_escape_string($_GET['est']);
			}else{
				$est = 180;
			}
			
			if(@$_GET['sud']){
				$sud = mysql_real_escape_string($_GET['sud']);
			}else{
				$sud = -90;
			}
			
		
				$res = mysql_query("select * from evenimente where (data_eveniment BETWEEN date('$de_la_data') AND date('$pana_la_data')) AND mag >= {$magnitudine} AND (adancime BETWEEN {$adancimeDeLa} AND {$adancimePanaLa}) AND (lat BETWEEN {$sud} AND {$nord})	AND (lon BETWEEN {$vest} AND {$est}) order by data_eveniment desc") or die(mysql_error());
		
				$arr = array();
				while($row = mysql_fetch_array($res)){
					$arr[] =  $row;
				}
			
				header('Content-Type: application/json');
				echo "callback(".json_encode($arr) .")";

		
				
			
			break;
			
			default:
					$arr = array("succes" => "0","error"=>"Actiune irelevanta");
					echo json_encode($arr);
					header('HTTP/1.1 401 Unauthorized', true, 401);
			break;
		}
	}

?>
